// pages/people-home/people-home.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    people_level: '', //名片等级
    mine_level: '',   //自己等级
    hasCard:true, //已创建名片
    navIdx: '1',
    id: '',
    btmList: [
      { img1: '/img/card-logo1.png', img: '/img/card-logo.png', title: '名片' },
      { img1: '/img/card-home1.png', img: '/img/card-home.png', title: '官网' },
      { img1: '/img/news-logo1.png', img: '/img/news-logo.png', title: '聊天' },
    ],
    productList: [
      
    ],
    markers:[{
      width: 20,
      height: 35,
      longitude: '',
      latitude: '',
      iconPath:'/img/mark.png',
    }]
  },


  selectedNavTap: function (e) { //底部跳转
    let that = this;
    let id = that.data.id;
    let idx = e.currentTarget.dataset.idx;
    console.log(idx);
    if (idx == that.data.navIdx) {
      return;
    }
    if (idx == 2 && that.data.userInfo.is_card == 0) { //未创建名片
      that.setData({
        hasCard: false,
      })
      return;
    }
    that.setData({
      navIdx: idx
    })
    if (idx == 0) { //名片
      wx.redirectTo({
        url: '/pages/people-card/people-card?id=' + id,
      })
    } else if (idx == 1) { //官网
      wx.redirectTo({
        url: '/pages/people-home/people-home?id=' + id,
      })
    } else { //聊天
      wx.redirectTo({
        url: '/pages/chat/chat?id=' + id,
      })
    }
  },


  createCardTap: function (e) { //创建名片
    app.confirmLogin(
      function () {
        wx.navigateTo({
          url: '/pages/card-msg-add/card-msg-add',
        })
      })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    that.setData({
      id
    },()=>{
      that.getHome();
    })
    
    that.getUserInfo();//获取用户是否创建名片
  },

  getUserInfo: function () {//获取用户是否创建名片
    var that = this
    var user_id = wx.getStorageSync('userId')
    wx.request({
      url: app.globalData.host + 'member/getUserInfo',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        user_id: user_id
      },
      success: function (res) {
        console.log(res)
        console.log(res.data.data.user)
        that.setData({
          userInfo: res.data.data.user,
          mine_level: res.data.data.user.level || 0,
        })
        wx.hideLoading();
      }
    })
  },

  getHome() {//获取官网信息
    var that = this
    wx.request({
      url: app.globalData.host + 'website/websiteDetail',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        user_id: that.data.id,
      },
      success(res) {
        console.log(res)
       
        if (!res.data.data.content){
          wx.showModal({
            title: '温馨提示',
            content: '该用户暂未创建官网',
            showCancel:false,
            confirmText:'返回',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })
          return;
        }
        console.log(JSON.parse(res.data.data.content))
        var content = JSON.parse(res.data.data.content)
        var target = [];
        var target = content.target.split(' ');
        that.setData({
          add_comp_logo:content.add_comp_logo,
          add_banner_img: content.add_banner_img,
          add_about_img: content.add_about_img,
          add_honour_img: content.add_honour_img,
          target: target,
          aboutText: content.aboutText,
          awardText: content.awardText,
          phone: content.phone,
          addressName: content.addressName,
          addressDetail: content.addressDetail,
          latitude: content.latitude,
          longitude: content.longitude,
          'markers[0].latitude': content.latitude,
          'markers[0].longitude': content.longitude,
          company_id: res.data.data.id,

          user: res.data.data.member,
          people_level: res.data.data.member.level.level||0,
          productList: res.data.data.product.data,
        })
      },
      fail(err) { },
      complete(comp) { },
    })

  },

  showModal: function () {
    var that = this;
    that.setData({
      hasCard: true,
      checkCard: true
    })
  },

  lookCardTap: function () { //查看名片
    var that = this;
    var id = that.data.id;
    wx.navigateTo({
      url: '/pages/people-card/people-card?id=' + id,
    })
  },

  // 跳转到商品详情
  openProductDetail: function (e) { //打开产品详情
    let id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/product-detail/product-detail?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})